#include "securityalgorithm.h"

SecurityAlgorithm::SecurityAlgorithm()
{

}

void SecurityAlgorithm::D587PSS(const uint8_t *seedText, uint8_t *keyText)
{
    uint8_t Idx = 0, u8Bitt = 0;
    uint8_t seed[D587_SEED_LEN], key[D587_SEED_LEN];
    const uint8_t AppKeyConst[D587_SEED_LEN] = {0x76, 0x59, 0x77, 0x8A};
    uint32_t u32Key = 0;

    for (Idx = 0; Idx < D587_SEED_LEN; Idx++)
    {
        key[Idx] = seedText[Idx] ^ AppKeyConst[Idx];
    }

    for (Idx = 0; Idx < D587_SEED_LEN; Idx++)
    {
        u8Bitt = BITTRANS(seedText[3 - Idx]);
        seed[Idx] = u8Bitt;
    }

    for (Idx = 0; Idx < D587_SEED_LEN; Idx++)
    {
        keyText[Idx] = seed[Idx] ^ AppKeyConst[Idx];
    }

    u32Key = (keyText[0] << 24 | keyText[1] << 16 | keyText[2] << 8 | keyText[3]);
    u32Key += (key[0] << 24 | key[1] << 16 | key[2] << 8 | key[3]);

    for (Idx = 0; Idx < D587_SEED_LEN; Idx++)
    {
        keyText[Idx] = u32Key >> (3 - Idx) * 8;
    }
}
